package org.lzuploader.gui.listeners.servers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.WServers;

/** This class is used when user changes server's parameters in server window.
 * 
 * @author Emmanuel Cron
 *
 */
public class ParametersModifyListener implements ModifyListener
{
    /** The logger for this class. */
    private static Log log = LogFactory.getLog(ParametersModifyListener.class);
    
    /** Called when user modifies the widget.
     * @param e Associated event
     */
    @Override
    public void modifyText(final ModifyEvent e)
    {
        log.debug("User has modified some server's parameters in the window.");
        
        log.trace("Fetching data.");
        
        Shell wServersShell = null;
        for (int i = 0 ; i < e.display.getShells().length ; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WServers)
            {
                wServersShell = e.display.getShells()[i];
                break;
            }
        }
        
        final Object data = wServersShell.getData();
        WServers servers;
        
        if (data instanceof WServers)
        {
            servers = (WServers) data;
        }
        else
        {
            log.trace("getData() is not an instance of WServers. Returning.");
            return;
        }
        
        log.trace("Disabling add button if needed.");
        
        if (servers.getAddButton().getEnabled())
        {
            servers.getAddButton().setEnabled(false);
        }
    }

}
